/**
 * Copyright (c) 2002-2011 旭鸣信息
 * All rights reserved. 
 * File：CouponContorller.java
 * History:
 *       May 13, 2012: Initially created, liuzhi.
 */
package com.xteam.ecsystem.contorller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.tangkf.utils.bean.ReqHelper;
import com.xteam.ecsystem.entities.BaseCouponInfo;
import com.xteam.ecsystem.service.admin.CouponService;
import com.xteam.ecsystem.service.admin.SystemLogService;
import com.xteam.ecsystem.utils.ConstantLocation;
import com.xteam.ecsystem.utils.StringUtil;

/**
 * 购物券控制枢纽站
 * @author liuzhi
 *
 */
@Controller
public class CouponContorller {

	@Autowired
	private CouponService couponService;
	
	@Autowired
	private SystemLogService logService;
	
	public void setToBeJsonType(HttpServletRequest request, HttpServletResponse response) {
		response.setContentType("application/json;charset=UTF-8");
	};
	
	@RequestMapping("/getCouponList.do")
	public void getCouponList(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		
		int pageIndex = ReqHelper.getRequestInt(request, "pageIndex");
		int pageSize = ReqHelper.getRequestInt(request, "pageSize");
		String bciName = ReqHelper.getRequestString(request, "bciName");
		String bciState = ReqHelper.getRequestString(request, "bciState");
		
		String strList = this.couponService.getCouponList(pageIndex, pageSize, bciName, bciState);
		response.getWriter().write(strList);
	}
	
	@RequestMapping("/saveOrUpdateCoupon.do")
	public void saveOrUpdateObject(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String jsonStr = ReqHelper.getRequestString(request, "couponInfo");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");
		String lslType = ConstantLocation.LOG_ADD;
		
		JSONObject obj = JSONObject.fromObject(jsonStr);
		BaseCouponInfo coupon = (BaseCouponInfo)JSONObject.toBean(obj, BaseCouponInfo.class);
		
		if(StringUtil.isNotNullOrEmpty(coupon.getBciId())){
			lslType = ConstantLocation.LOG_MODIFY;
		}
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_COUPON_MGR, 
				"saveOrUpdateObject", lslType, "BaseCouponInfo");
		
		this.couponService.saveOrUpdateObject(coupon, coupon.getBciId());
	}
	
	@RequestMapping("/getCouponInfoById.do")
	public void getCouponInfoById(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		
		String bciId = ReqHelper.getRequestString(request, "bciId");		
		String strInfo = this.couponService.getCouponInfoById(bciId);
		
		response.getWriter().write(strInfo);
	}
	
	@RequestMapping("/updateCouponStateById.do")
	public void updateCouponStateById(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String bciId = ReqHelper.getRequestString(request, "bciId");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");

		this.couponService.updateCouponStateById(bciId);

		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_COUPON_MGR, 
				"updateCouponStateById", ConstantLocation.LOG_Edit_STATE, "BaseCouponInfo");
	}
	
	@RequestMapping("/delCouponByIds.do")
	public void delCouponByIds(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String bciIds = ReqHelper.getRequestString(request, "bciIds");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");

		this.couponService.delCouponByIds(bciIds);
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_COUPON_MGR, 
				"delCouponByIds", ConstantLocation.LOG_DELETE, "BaseCouponInfo");

	}
	
}
